package com.letoy.edu.service.Impl;

import com.letoy.edu.dao.UserMapper;
import com.letoy.edu.entity.User;
import com.letoy.edu.service.UserService;

import com.letoy.edu.utils.BCryptPasswordEncoderUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    BCryptPasswordEncoderUtil bCryptPasswordEncoderUtil;

    @Override
    public String getUserIdByNamePassword(String name, String password) {
        User user = userMapper.getUserByName(name);
        if (!bCryptPasswordEncoderUtil.matches(user.getPassword(), password)) {
            return user.getUserId();
        } else {
            return null;
        }

    }

    @Override
    public User getUserByName(String name) {
        return userMapper.getUserByName(name);
    }

    @Override
    public int insertToken(String id, String token) {
        return userMapper.insertToken(id, token);
    }

    @Override
    public User loginUser(User user) {
        User resUser = userMapper.loginUser(user);
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        System.out.println(user.getEmail());

        if (!bCryptPasswordEncoderUtil.matches(resUser.getPassword(), user.getPassword())) {
            return resUser;
        } else {
            return null;
        }
    }

    @Override
    public User getUserByEmail(String email) {
        return null;
    }
}
